/******************************************************************************
 *
 * Copyright (c) 1999 Palm Computing, Inc. or its subsidiaries.
 * All rights reserved.
 *
 * File: Starter.cpp
 *
 *****************************************************************************/

#include <PalmOS.h>
#include "StarterRsc.h"


/***********************************************************************
 *
 * FUNCTION:    SayHello
 *
 * DESCRIPTION: This routine draws "Hello World!" on the form
 *
 * PARAMETERS:  none
 *
 * RETURNED:    none
 *
 ***********************************************************************/
static void SayHello()
{
	short nCharWidth = 0;
	short width = 0, height = 0;
	Char* pText = (Char*) MemPtrNew(13);  // allocate 13 bytes
			
	// copy Hello World! into the char ptr
	StrCopy(pText, "Hello World!");
	
	// get the width of the string		
	nCharWidth = FntCharsWidth(pText, StrLen(pText));  
	// get the width and height of the string
	WinGetWindowExtent(&width, &height);
	
	// draw the text in the center
	WinDrawChars(pText, StrLen(pText), (width/2) - (nCharWidth/2), height/2);

	// free the text memory
	MemPtrFree(pText);
}


/***********************************************************************
 *
 * FUNCTION:    MainFormButtonHandler
 *
 * DESCRIPTION: This routine is the event handler for buttons on the 
 *				Main form.
 *
 ***********************************************************************/
static Boolean MainFormButtonHandler(FormPtr formP, EventPtr eventP)
{
	Boolean handled = false;
	
	switch (eventP->data.ctlEnter.controlID)
	{
		case MainSayHelloButton:
			SayHello();
			handled = true;
			break;

		default:
			break;      
	}
	
	return handled;
}


/***********************************************************************
 *
 * FUNCTION:    MainFormHandleEvent
 *
 * DESCRIPTION: This routine is the event handler for the 
 *              "MainForm" of this application.
 *
 * PARAMETERS:  eventP  - a pointer to an EventType structure
 *
 * RETURNED:    true if the event has handle and should not be passed
 *              to a higher level handler.
 *
 ***********************************************************************/
static Boolean MainFormHandleEvent(EventPtr eventP)
{
   	Boolean handled = false;
   	FormPtr frmP = FrmGetActiveForm();
   	
	switch (eventP->eType) 
	{
		case frmOpenEvent:
			FrmDrawForm(frmP);
			handled = true;
			break;

		case ctlSelectEvent:
			handled = MainFormButtonHandler(frmP, eventP);
			break;
			
		default:
			break;	
	}
	
	return handled;
}


/***********************************************************************
 *
 * FUNCTION:    AppHandleEvent
 *
 * DESCRIPTION: This routine loads form resources and set the event
 *              handler for the form loaded.
 *
 * PARAMETERS:  event  - a pointer to an EventType structure
 *
 * RETURNED:    true if the event has handle and should not be passed
 *              to a higher level handler.
 *
 ***********************************************************************/
static Boolean AppHandleEvent(EventPtr eventP)
{
	UInt16 formId;
	FormPtr frmP;
	Boolean bRetVal = false;

	if (eventP->eType == frmLoadEvent)
	{
		// Load the form resource.
		formId = eventP->data.frmLoad.formID;
		frmP = FrmInitForm(formId);
		FrmSetActiveForm(frmP);

		// Set the event handler for the form.  The handler of the currently
		// active form is called by FrmHandleEvent each time is receives an
		// event.
		switch (formId)
		{
			case MainForm:
				FrmSetEventHandler(frmP, MainFormHandleEvent);
				break;
	
			default:
				break;
		}
		
		bRetVal = true;
	}
	
	return bRetVal;
}


/***********************************************************************
 *
 * FUNCTION:    AppEventLoop
 *
 * DESCRIPTION: This routine is the event loop for the application.  
 *
 * PARAMETERS:  nothing
 *
 * RETURNED:    nothing
 *
 ***********************************************************************/
static void AppEventLoop(void)
{
	UInt16 error;
	EventType event;

	do 
	{
		EvtGetEvent(&event, evtWaitForever);

		if (! SysHandleEvent(&event))
			if (! MenuHandleEvent(0, &event, &error))
				if (! AppHandleEvent(&event))
					FrmDispatchEvent(&event);

	} while (event.eType != appStopEvent);
}


/***********************************************************************
 *
 * FUNCTION:    PilotMain
 *
 * DESCRIPTION: This is the main entry point for the application.
 *
 * PARAMETERS:  cmd - word value specifying the launch code. 
 *              cmdPB - pointer to a structure that is associated with the launch code. 
 *              launchFlags -  word value providing extra information about the launch.
 * RETURNED:    Result of launch
 *
 ***********************************************************************/
UInt32 PilotMain( UInt16 cmd, MemPtr cmdPBP, UInt16 launchFlags)
{
	switch (cmd)
	{
		case sysAppLaunchCmdNormalLaunch:
			FrmGotoForm(MainForm);
			AppEventLoop();
			FrmCloseAllForms();
			break;
		
		default:
			break;
	}
	
	return errNone;
}

